package com.scw.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.scw.model.LinkInfo;
import com.scw.model.SysUser;
import com.scw.model.Tip;
import com.scw.service.LinkInfoService;
import com.scw.service.TipService;
import com.scw.util.CommonNameUtil;

/**
 * 收藏管理类
 * @author 王华聪
 *
 */
public class LinkAction {
	private LinkInfo linkInfo;
	private Tip tip;
	private LinkInfoService linkService;
	private TipService  tipService;
	/**
	 * 添加收藏
	 * @return
	 */
	public String add(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String title=request.getParameter("title");
		String url=request.getParameter("url");
		String intro=request.getParameter("intro");
		linkInfo=new LinkInfo();
		linkInfo.setLinkIntro(intro);
		linkInfo.setLinkTitle(title);
		linkInfo.setLinkAddress(url);
		request.setAttribute("linkInfo", linkInfo);
		return "add";
	}
	
	/**
	 * 保存收藏
	 * @return
	 */
	public String save(){
		HttpSession session =ServletActionContext.getRequest().getSession();
		SysUser sysUser=(SysUser) session.getAttribute(CommonNameUtil.SessionUser);
		Tip tip=new Tip();
		tip.setSysUser(sysUser);
		linkInfo.setKeepDate(new Date());
		linkInfo.setLinkType("1");
		//如果标签没写则加入”其他“默认标签。
		if(linkInfo.getTip()!=null && "".equals(linkInfo.getTip().getTip_name())){
			tip.setTip_name(CommonNameUtil.TIP_NAME);
			tip=tipService.get(tip);
			//没有默认标签先添加一个默认标签
			if(tip ==null){
				tip=new Tip();
				tip.setTip_name(CommonNameUtil.TIP_NAME);
				tip.setSysUser(sysUser);
				tip.setCreate_date(new Date());
				tip=tipService.save(tip);
			}
		}else{
			tip.setTip_name(linkInfo.getTip().getTip_name());
			tip=tipService.get(tip);
			//如果已经存在标签则直接加关联
			//没有则添加新的标签
			if(tip ==null){
				tip=new Tip();
				tip.setTip_name(linkInfo.getTip().getTip_name());
				tip.setSysUser(sysUser);
				tip.setCreate_date(new Date());
				tip=tipService.save(tip);
			}
		}
		linkInfo.setTip(tip);
		linkService.saveLinkInfo(linkInfo);
		return "save";
	}
	
	/**
	 * 编辑收藏
	 * @return
	 */
	public String edit(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("id");
		linkInfo=linkService.getLinkInfoById(Long.valueOf(id));
		request.setAttribute("linkInfo", linkInfo);
		return "edit";
	}
	
	/**
	 * 更新收藏
	 * @return
	 */
	public String update(){
		linkService.updateLinkInfo(linkInfo);
		return "list";
	}
	
	/**
	 * 删除收藏
	 * @return
	 */
	public String del(){
		HttpServletRequest req=ServletActionContext.getRequest();
		String[] ids=(String[]) req.getAttribute("checkboxs");
		LinkInfo linkInfo=new LinkInfo();
		for(String id:ids){
			linkInfo.setId(Long.valueOf(id));
			linkService.delLinkInfo(linkInfo);
		}
		return "list";
	}
	
	/**
	 * 全部收藏list（管理员用）
	 * @return
	 */
	public String all(){
		HttpServletRequest req=ServletActionContext.getRequest();
		List<LinkInfo> list= new ArrayList<LinkInfo>();
		list=linkService.getAllLink();
		req.setAttribute("linkList", list);
		return "all";
	}
	/**
	 * 根据标签显示收藏list
	 * @return
	 */
	public String list(){
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpSession session =req.getSession();
		SysUser sysUser=(SysUser) session.getAttribute(CommonNameUtil.SessionUser);
		String tid=req.getParameter("id");
		List<Tip> tipList=tipService.findByUserId(sysUser.getId());
		//没有指定哪个标签，默认由数据库查询
		if(tipList.size()>0){
		if(tid==null ||"".equals(tid)){
			tip=tipList.get(0);
		}else{
			tip=new Tip();
			tip.setId(Long.valueOf(tid));
		}
		List<LinkInfo> linkList=linkService.listByTip(tip);
		req.setAttribute("linkList", linkList);
		}
		req.setAttribute("tipList", tipList);
		return "listByTip";
	}
	
	public Object getModel(){
		int i=0;
		int b=1;
		return this;
	}

	public LinkInfo getLinkInfo() {
		return linkInfo;
	}

	public void setLinkInfo(LinkInfo linkInfo) {
		this.linkInfo = linkInfo;
	}

	public LinkInfoService getLinkService() {
		return linkService;
	}

	public void setLinkService(LinkInfoService linkService) {
		this.linkService = linkService;
	}

	public Tip getTip() {
		return tip;
	}

	public void setTip(Tip tip) {
		this.tip = tip;
	}

	public TipService getTipService() {
		return tipService;
	}

	public void setTipService(TipService tipService) {
		this.tipService = tipService;
	}

}
